package com.sali.schedule;

import com.sali.config.MockConfig;
import com.sali.handler.DataHandler;
import com.sali.handler.FileHandler;
import com.sali.handler.MessageStatistics;
import com.sali.handler.SocketHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @description:
 * @author: sali
 * @date: 2025/2/21 下午11:05.
 * @version: 1.0
 */
@Component
@Slf4j
public class DataProduceSchedule {

    @Autowired
    private SocketHandler socketHandle;

    @Autowired
    private DataHandler dataHandler;

    @Autowired
    private FileHandler fileHandler;

    @Autowired
    private MockConfig mockConfig;

    @Value("classpath:${mock-config.file-path}")
    private Resource resourceFile;

    /**
     * description: socket 数据生产并发送
     **/
    @Scheduled(cron = "0/1 * * * * ?")
    public void handle() {
        if (!socketHandle.isConnectionFlag()) {
            return;
        }

        long total = MessageStatistics.getSendMsgCount().get();
        if (total > mockConfig.getMaxCount()) {
            log.info("已完成指定数量生产！！ {}条", total);
            return;
        }

        log.info("The TCP Data is begin processed");
        long startTime = System.currentTimeMillis();
        String data = fileHandler.handle(resourceFile);
        dataHandler.send(data);
        log.info("End of data processing, time = {}/ms", (System.currentTimeMillis() - startTime));
    }

}
